package com.jinlejia.erp.ui.mine.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ItemMyIntoBinding;
import com.jinlejia.erp.utils.StringUtil;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.ArrayList;
import java.util.Map;

/**
 * 我的分成（adapter）
 */

public class MyIntoAdapter extends YfListAdapter<Map<String, Object>> {


    private Context context;

    public MyIntoAdapter(ArrayList<Map<String, Object>> data) {
        super(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        context = parent.getContext().getApplicationContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_into, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);
        TextView txt = view.findViewById(R.id.id_tips);
        txt.setText("暂无数据");
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        ItemMyIntoBinding binding = DataBindingUtil.getBinding(viewHolder.itemView);
        final Map<String, Object> map = mData.get(position);

        if (map.containsKey("contractCode")) {//合同编号
            if (map.get("isTradeyUser").toString().equals("1")) {
                binding.contractNumber.setTextColor(context.getResources().getColor(R.color.blue));
            } else {
                binding.contractNumber.setTextColor(context.getResources().getColor(R.color.hintColor));
            }
        }
        if (map.containsKey("reason")) {
            binding.charge.setText(map.get("reason").toString());
        }
        binding.contractNumber.setText(map.get("contractCode").toString());
        if (map.containsKey("contractType")) { //合同类型
            binding.state.setTextColor(context.getResources().getColor(R.color.red));
            switch (map.get("contractType").toString()) {
                case "0":
                    binding.state.setText("买卖");
                    break;
                case "1":
                    binding.state.setText("租赁");
                    break;
                case "2":
                    binding.state.setText("一手房");
                    break;
                case "3":
                    binding.state.setText("垫资");
                    break;
                case "4":
                    binding.state.setText("金钥匙");
                    break;
                default:
                    break;
            }
        }
        if (map.containsKey("address"))  //物业地址
            binding.propertyAddress.setText(map.get("address").toString());
        //分成人
        if (map.containsKey("userName")) {
            binding.fenchengrenLayout.setVisibility(View.VISIBLE);
            binding.propertyFenchengren.setText(StringUtil.valueOf(map.get("userName")));
        } else {
            binding.fenchengrenLayout.setVisibility(View.GONE);
        }
        if (map.containsKey("scale"))  //分成比例
            binding.chargeBroker.setText(map.get("scale").toString().concat("%"));
        if (map.containsKey("receivables"))  //分成应收
            binding.intoReceivable.setText(map.get("receivables").toString().concat("元"));
        if (map.containsKey("paidAccount"))  //分成实收
            binding.intoReceived.setText(map.get("paidAccount").toString().concat("元"));

        if (map.containsKey("commissionState"))
            switch (Integer.parseInt(map.get("commissionState").toString())) {
                case 0:
                    binding.commissionState.setText("未结佣");
                    break;
                case 1:
                    binding.commissionState.setText("收佣结佣");
                    break;
                case 2:
                    binding.commissionState.setText("过户结佣");
                    break;
                case 3:
                    binding.commissionState.setText("结件结佣");
                    break;
                case 4:
                    binding.commissionState.setText("批复结佣");
                    break;
                case 5:
                    binding.commissionState.setText("申请结佣");
                    break;
                case 6:
                    binding.commissionState.setText("已解约");
                    break;
            }

        binding.getRoot().setTag(mData.get(position));
        binding.contractNumber.setOnClickListener(view -> listener.skip(map.get("isTradeyUser").toString(), map.get("contractCode").toString()));
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new FooterViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateErrorViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.error_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindFooterViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindFooterViewHolder(holder, position);
        if (mFooters.get(0).equals(R.string.is_all)) {
            ((FooterViewHolder) holder).txt.setText(R.string.is_all);
            ((FooterViewHolder) holder).txt.setVisibility(View.VISIBLE);
            ((FooterViewHolder) holder).progress.setVisibility(View.GONE);
        } else {
            ((FooterViewHolder) holder).txt.setVisibility(View.GONE);
            ((FooterViewHolder) holder).progress.setVisibility(View.VISIBLE);
        }
    }

    static final class FooterViewHolder extends RecyclerView.ViewHolder {
        TextView txt;
        AVLoadingIndicatorView progress;

        public FooterViewHolder(View itemView) {
            super(itemView);
            txt = itemView.findViewById(R.id.txt);
            progress = itemView.findViewById(R.id.progress);
        }
    }

    static final class ViewHolder extends RecyclerView.ViewHolder {


        public ViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }

    public interface OnInToClickListener {
        void skip(String state, String Code);
    }

    public OnInToClickListener listener;

    public void OnIntoClickListener(OnInToClickListener listener) {
        this.listener = listener;
    }
}
